<template>
	<view class="container">
		<scroll-view scroll-y class="content" @scrolltolower="scrollBottom">
			<view class="time font_title">
				<text>{{ getCurrentDate().month }}</text><text
					style="font-size: 46rpx;">/</text><text>{{ getCurrentDate().date }} </text>
			</view>
			<view class="hot_news flex">
				<view class="news_item" v-for="item in newList.slice(0,4)" @tap="goToNewsDetail(item.url)">
					<view class="image" :style="{background:`url(${item.image}) no-repeat center / cover`}"></view>
					<view class="detail flex_between">
						<view class="writer font_title">{{item.source}}</view>
						<view class="news_time">{{ formattedDate(item.createtime) }}</view>
					</view>
					<view class="text el2">
						{{item.title}}
					</view>
				</view>
			</view>
			<view class="news_list">
				<view class="news_item flex" v-for="item in newList" @tap="goToNewsDetail(item.url)">
					<view class="image" :style="{background:`url(${item.image}) no-repeat center / cover`}"></view>
					<view class="detail">
						<view class="title flex_between">
							<view class="writer font_title">{{item.source}}</view>
							<view class="news_time">{{ formattedDate(item.createtime) }}</view>
						</view>
						<view class="text el2">
							{{item.title}}
						</view>
					</view>
				</view>
			</view>
		</scroll-view>
		<tabbarView id="3"></tabbarView>
	</view>
</template>

<script>
	import tabbarView from "@/components/tabbar.vue"
	import {
		NewListApi
	} from '@/api/api.js'
	export default {
		components: {
			tabbarView
		},
		data() {
			return {
				form: {
					page: 1,
				},
				time: new Date().getTime(),
				newList: [],
			};
		},
		onShow() {
			this.getNewList()
		},
		methods: {
			// 滚动触底
			scrollBottom() {
				console.log("滚动到底部")
				this.form.page++
				this.getNewList()
			},
			// 获取日期
			getCurrentDate() {
				const currentDate = new Date();
				const date = currentDate.getDate();
				const month = currentDate.getMonth() + 1; // JavaScript 中月份是从 0 开始的，所以要加 1

				return {
					date,
					month,
				};
			},
			goToNewsDetail(url) {
				console.log("Clicked on news item. URL:", url);
				// 使用 uniapp 导航 API 进行跳转
				if (url) {
					window.open(url)({
						url: url
					});
				}
			},
			formattedDate(createtime) {
				const date = new Date(createtime);
				return date.toLocaleDateString();
			},
			getNewList() {
				NewListApi(this.form).then(res => {
					if (this.form.page == 1) {
						this.newList = res.data.data.data
					} else {
						this.newList = [...this.newList, ...res.data.data.data]
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		.content {
			.time {
				font-size: 60rpx;
				margin-bottom: 40rpx;
			}

			.hot_news {
				overflow: auto;

				.news_item {
					width: 460rpx;
					border: 2rpx solid rgba(237, 237, 237, .46);
					flex-shrink: 0;
					border-radius: 20rpx;
					padding: 8rpx;
					margin-right: 20rpx;

					.image {
						width: 100%;
						height: 320rpx;
						border-radius: 20rpx;
						overflow: hidden;
						opacity: 1;
						transition: opacity 0.5s ease-in-out 0s;
						box-shadow: 0px 6rpx 16rpx 0 rgba(0, 0, 0, .16);
					}

					.detail {
						padding: 20rpx 20rpx 0;
						font-size: 26rpx;
						opacity: .5;
					}

					.text {
						padding: 0 20rpx 20rpx;
						height: 100rpx;
					}
				}
			}

			.news_list {
				margin-top: 30rpx;

				.news_item {
					border: 2rpx solid rgba(237, 237, 237, .46);
					margin-bottom: 10rpx;
					padding: 8rpx;
					border-radius: 8rpx;

					.image {
						width: 140rpx;
						height: 140rpx;
						box-shadow: 0 6rpx 16rpx 0 rgba(0, 0, 0, .16);
						border-radius: 20rpx;
						overflow: hidden;
					}

					.detail {
						padding: 20rpx;
						flex: 1;
						overflow: hidden;

						.title {
							opacity: .5;
						}

						.text {
							font-size: 30rpx;
						}
					}
				}
			}
		}
	}
</style>